﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CMS.Domain.Services
{
    public class DisableService
    {
        public CPPEI.ServiceResult DisableArticle(Guid id)
        {
            using (CMSDbContext dbContext = new CMSDbContext())
            {
                EntityRepository<Entities.EArticle> rep = new EntityRepository<Entities.EArticle>(dbContext);

                Entities.EArticle article = rep.Get(p => p.Id == id);
                if (article == null)
                {
                    return new CPPEI.ServiceResult(0);
                }

                if(!string.IsNullOrEmpty(article.PublishedFile))
                {
                    try
                    {
                        File.Delete(article.PublishedFile);
                    }
                    catch(Exception ex)
                    {
                    }
                }

                article.PublishedFile = null;
                article.PublishedTime = null;
                article.PublishedUrl = null;
                article.DisplayPublishedTime = null;
                article.IsEnabled = false;

                rep.Update(article);

                try
                {
                    dbContext.SaveChanges();
                    return new CPPEI.ServiceResult();
                }
                catch (Exception ex)
                {
                    return new CPPEI.ServiceResult(0);
                }
            }
        }
    }
}
